#include "ObjectManager.h"

#include "../Utility/Utility.h"
#include "../Factory/StackFactory.h"

////////////////////////////////////////////////////////////////////////////////////////////////////
/// <Summary>	Initializes the object manager </Summary>
////////////////////////////////////////////////////////////////////////////////////////////////////
void ObjectManager::Initialize()
{
	
}

////////////////////////////////////////////////////////////////////////////////////////////////////
/// <Summary>	Shuts down the object manager </Summary>
////////////////////////////////////////////////////////////////////////////////////////////////////
void ObjectManager::Shutdown()
{

}

void ObjectManager::AddObject(IObject* obj)
{
	currentObjects.Push_Front(obj);
}

void ObjectManager::RemoveObject(IObject* obj)
{
	for(ObjectList::Iterator iter(currentObjects); !iter.End(); ++iter)
	{
		if((*iter) == obj)
		{
			currentObjects.Erase(iter);	
			break;
		}
	}

	DeleteObject(obj);
}

void ObjectManager::RemoveObject(const char* name)
{
	unsigned long hash = Utility::CreateHash(name);

	for(ObjectList::Iterator iter(currentObjects); !iter.End(); ++iter)
	{
		if((*iter)->GetName().GetIdentity() == hash)
		{
			currentObjects.Erase(iter);
			DeleteObject((*iter));
			break;
		}
	}
}

IObject* ObjectManager::FindObject(const char* name)
{
	unsigned long hash = Utility::CreateHash(name);

	for(ObjectList::Iterator iter(currentObjects); !iter.End(); ++iter)
	{
		if((*iter)->GetName().GetIdentity() == hash)
		{
			return (*iter);
		}
	}

	return NULL;
}

void ObjectManager::Update(float delta)
{
	for(ObjectList::Iterator iter(currentObjects); !iter.End(); ++iter)
	{
		(*iter)->Update(delta);
	}
}

void ObjectManager::DeleteObject(IObject* obj)
{
	((StackFactory<void>*)registeredObjects[obj->GetObjectID().GetIdentity()])->DeleteObject(obj);
}

void ObjectManager::ClearObjects()
{
	for(ObjectList::Iterator iter(currentObjects); !iter.End(); ++iter)
	{
		DeleteObject((*iter));
	}

	currentObjects.Clear();
}